/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ec.com.farmaciassantamartha.comprobantes.portal.mb;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Maestria
 */
@ManagedBean
@RequestScoped
public class MediaManager {

//    @EJB
//    private MediaService service;

    public StreamedContent getStream() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        } else {
            // So, browser is requesting the media. Return a real StreamedContent with the media bytes.
            String pathPdf = context.getExternalContext().getRequestParameterMap().get("pathPdf");
//            Media media = new Media();
            InputStream stream = new FileInputStream(pathPdf);
            return new DefaultStreamedContent(stream, "application/pdf");  
//            return new DefaultStreamedContent(new ByteArrayInputStream(media.getBytes()));
        }
    }
}
